/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package polyhedron;

import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.control.AbstractControl;

/**
 *
 * @author ryan
 */
public class CameraAttachmentState extends AbstractAppState
{    
    private AppStateManager stateManager;
    private Application app;
    private Node attachment;
    
    @Override
    public void initialize(AppStateManager stateManager, Application app)
    {
        super.initialize(stateManager, app);
        this.stateManager = stateManager;
        this.app = app;
    }
    
    public void attach(Node attachment)
    {
        this.attachment = attachment;
    }

    @Override
    public void update(float tpf)
    {
        super.update(tpf); //To change body of generated methods, choose Tools | Templates.

        int width = app.getCamera().getWidth();
        int height = app.getCamera().getHeight();
        Vector2f click2d = app.getInputManager().getCursorPosition();
        Vector2f screenPos = new Vector2f(width, height);
        Vector3f click3d = app.getCamera().getWorldCoordinates(new Vector2f(width/2, height/2), 0f).clone();
        Ray ray = new Ray(click3d, app.getCamera().getDirection());
        if(attachment != null)
        {
            Vector3f location = click3d.add(ray.getDirection().mult(3));
            attachment.setLocalTranslation(location);
        }
        
    }

    @Override
    public void render(RenderManager rm)
    {
        super.render(rm); //To change body of generated methods, choose Tools | Templates.
        
    }
    
}
